<?php
if(!defined("USER_TABLE")) {
   define("USER_TABLE", 'mt_user');
}
if(!defined("TRACK_TABLE")) {
   define("TRACK_TABLE", 'mt_track');
}
if(!defined("ARTIST_TABLE")) {
   define("ARTIST_TABLE", 'mt_artist');
}
if(!defined("RELEASE_TABLE")) {
   define("RELEASE_TABLE", 'mt_release');
}
if(!defined("USER2TRACK_TABLE")) {
   define("USER2TRACK_TABLE", 'mt_user2track');
}
if(!defined("ARTICLE_TABLE")) {
   define("ARTICLE_TABLE", 'mt_article');
}
if(!defined("TAGS_TABLE")) {
   define("TAGS_TABLE", 'mt_tags');
}
if(!defined("USER2FAV_TABLE")) {
   define("USER2FAV_TABLE", 'mt_user2fav');
}

abstract class Model {

  protected $attributes = array();
  protected $table_name;
  protected $SQL;
  public $id;
  
  protected function Model($attr=null){
    $this->set($attr);
    $this->SQL  = SQL::factory();
    if($this->table_name == null)
      throw new MTExcepetion('The db table is undefined');
  }
  
  public function set($attr){
    if($attr && is_array($attr)){
      foreach($attr as $name=>$val)
        $this->attributes[$name] = $val;
    }
  }
  
  public function reset($attr=null){
    $this->attributes = array();
    $this->set($attr);
  }
  
  public function getAttribute($name=null){
  	if($name) return $this->attributes[$name];
    return $this->attributes;
  }
  
  public function setAttribute($name, $val){
    $this->attributes[$name] = $val;
  }
  
  
  public function add(){
    if(! sizeof($this->attributes))
      throw new MTException('Cannot write empty model');
     
    $q1 = "INSERT INTO `{$this->table_name}` (";
    $q2 = " VALUES (";
    
    foreach($this->attributes as $name=>$val){
      if($val == null) continue;
      
      $val = mysql_escape_string($val);
      $q1 .= "`$name`,";
      $q2 .= (is_int($val) || $val=='NOW()')?$val:"'$val'";
      $q2 .= ',';
    }
    
    $this->SQL->query(substr( $q1, 0 , -1 ).') '. substr( $q2, 0 , -1 ).')');
    //$this->SQL->query("SELECT LAST_INSERT_ID() AS id;");
    // $this->SQL->fetchObject()->id;
    $this->id = (int)mysql_insert_id();
    return $this->id;
  }
   
  public function get($attr=null,$groupOrder=''){
    //array_intersect_assoc ( array $array1, array $array2 [, array $ ...] )
    $attr = $attr?$attr:$this->attributes;
    
    $q= "SELECT * FROM {$this->table_name} WHERE ";
    
    foreach($attr as $name=>$val){
      if($val == null) continue;
      
      $val = mysql_escape_string($val);
      $q .= "`$name`=";
      $q .= (is_int($val) || $val=='NOW()' || $val=='NULL')?$val:"'$val'";
      $q .= ' AND ';
    }
    //echo substr( $q, 0 , -5);
    $this->SQL->query(substr( $q, 0 , -5).' '.$groupOrder);
    
    $tmp_ids = array();
    $tmp_res = array();
    
    while($res = $this->SQL->fetchObject()){
      $tmp_ids[] = (int)$res->id;
      $tmp_res[] = $res;
    }
    
    $this->id = sizeof($tmp_ids)>1?$tmp_ids:$tmp_ids[0];
    
    return sizeof($tmp_res)>1?$tmp_res:$tmp_res[0];
  }
  /*
  * implements the SQL UPDATE operation
  * if you need to update something like SET a = 1+a
  * use this notation as $attr: array(a=>'#1+a')
  */
  public function update($attr,$insert_into_attr=true){
  	
    $q = "UPDATE {$this->table_name} SET ";
    
    foreach($attr as $name=>$val){
      if($val == null) continue;	
    
      $val = mysql_escape_string($val);
      $q .= "$name=";
      if(substr($val, 0 , 1)=='#')
        $q .= substr($val, 1);
      else
       $q .= is_int($val)?$val:"'$val'";
      $q .= ', ';
    }
    
    $q = substr( $q, 0 , -2 ).' WHERE ';
    
    foreach($this->attributes as $name=>$val){
      if($val == null || $val == 'NOW()') continue;
      
      $val = mysql_escape_string($val);
      $q .= "$name=";
      $q .= is_int($val)?$val:"'$val'";
      $q .= ' AND ';
    }
    $this->SQL->query(substr( $q, 0 , -5 ));
    if(!$this->SQL->numAffectedRows())
      throw new MTException("Updated attempt on NON-existing row");
    
    if($insert_into_attr){
      $this->set($attr);
    } 
  }
  
  public function delete($attr=null){
    
    $attr = $attr?$attr:$this->attributes;
    
    $q= "DELETE FROM {$this->table_name} WHERE ";
    
    foreach($attr as $name=>$val){
      if($val == null || $val == 'NOW()') continue;
      
      $val = mysql_escape_string($val);
      $q .= "$name=";
      $q .= is_int($val)?$val:"'$val'";
      $q .= ' AND ';
    }
    //echo substr( $q, 0 , strlen($q)-5);
    $this->SQL->query(substr( $q, 0 , -5 ));
    $this->reset(array());
  }
}

?>
